<?php

namespace app\site\controller;

use app\common\model\ShopGoodsSkuModel;
use app\common\model\ShopGroupbuyModel;
use think\Request;

class ShopGroupbuyController extends BaseController
{
    private $groupbuyModel;
    private $goodsSkuModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->groupbuyModel = new ShopGroupbuyModel();
        $this->goodsSkuModel = new ShopGoodsSkuModel();
    }

    /**
     * 团购商品列表
     */
    public function index()
    {
        $query          = $this->request->request();
        $map['site_id'] = $this->getSiteId();
        $status         = $this->request->param('status', -1);
        $groupbuyName   = $this->request->param('groupbuy_name');
        if ($status >= 0) {
            $map['status'] = $status;
        }
        if (!empty($groupbuyName)) {
            $map['groupbuy_name'] = ['like', '%' . $groupbuyName . '%'];
        }
        $list = $this->groupbuyModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('query', $query);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加团购商品
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'groupbuy_name|活动名称'  => 'require',
                'begin_time|开始日期'     => 'date',
                'end_time|结束日期'       => 'date',
                'sku_id|活动商品'         => 'require',
                'groupbuy_limit|限购数量' => 'number',
                'groupbuy_price|团购价格' => 'float',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            if($data['groupbuy_price']>$data['simple_price']){
                $this->error('团购价格必须小于销售价格！');
            }
            $info = $this->groupbuyModel->where('site_id',$data['site_id'])->where('sku_id',$data['sku_id'])->find();
            if($info){
                $this->error('该商品已参加采购活动！');
            }
            $data['begin_time'] = strtotime($data['begin_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $data['status'] = 1; //默认审核通过
            $this->groupbuyModel->addData($data);
            $this->goodsSkuModel->where('sku_id',$data['sku_id'])->setField(['promotion_type'=>4,'promotion_price'=>$data['promotion_price']]);
            $this->success('操作成功！',url('index'));
        }
        return $this->fetch();
    }

    /**
     * 删除抢购商品
     */
    public function del()
    {
        $groupbuyId    = $this->request->param('groupbuy_id/d');
        $skuid         = $this->request->param('sku_id/d');
        $goodsSkuModel = new ShopGoodsSkuModel();
        $goodsSkuModel->where('sku_id', $skuid)->setField(['promotion_type'=>0,'promotion_price'=>0]);  //标记对应商品活动状态
        $this->groupbuyModel->where('groupbuy_id', $groupbuyId)->delete();
        $this->success('操作成功！');
    }

}
